package com.lkwlw.tplms.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.mapper.TransShipmentStationMapper;
import com.lkwlw.tplms.model.Pager;
import com.lkwlw.tplms.model.TransShipmentStation;
import com.lkwlw.tplms.model.TransShipmentStationExample;
import com.lkwlw.tplms.model.TransShipmentStationExample.Criteria;
import com.opensymphony.xwork2.ActionSupport;

public class TransShipmentStationAction extends ActionSupport{
	private static final long serialVersionUID = -1520297798331774128L;
	
	private TransShipmentStationMapper transShipStatMapper;
	private TransShipmentStation transShipmentStation;
	private List<TransShipmentStation> transShipList;
	private Pager pager;
	private int page = 1;
	private String selectId;
	
	@Override
	public String execute() throws Exception {
		TransShipmentStationExample example = new TransShipmentStationExample();
		Criteria criteria = example.createCriteria();
		if (null != transShipmentStation.getStationName() 
				&& !"".equals(transShipmentStation.getStationName())) {
			criteria.andStationNameLike("%" + transShipmentStation.getStationName() + "%");
		}
		if (null != transShipmentStation.getStationType() 
				&& !"".equals(transShipmentStation.getStationType())) {
			criteria.andStationTypeEqualTo(transShipmentStation.getStationType());
		}
		if (null != transShipmentStation.getStationArea() 
				&& !"".equals(transShipmentStation.getStationArea())) {
			criteria.andStationAreaLike("%" + transShipmentStation.getStationArea() + "%");
		}
		example.setOrderByClause("station_name asc");
		getPagination(example);
		return SUCCESS;
	}
	
	private void clearTransShipmentStation(){
		transShipmentStation.setId(null);
		transShipmentStation.setRemark(null);
		transShipmentStation.setStationName(null);
		transShipmentStation.setStationType(null);
		transShipmentStation.setStationArea(null);
		transShipmentStation.setStationLocation(null);
		transShipmentStation.setStationAdmin(null);
		transShipmentStation.setStationAdminId(null);
	}
	
	public String add() throws Exception{
		selectId = "";
		clearTransShipmentStation();
		return SUCCESS;
	}
	public String edit() throws Exception{
		if(null != selectId && !"".equals(selectId)){
			transShipmentStation = transShipStatMapper.selectByPrimaryKey(selectId);
			selectId = transShipmentStation.getStationType();
		}
		
		return SUCCESS;
	}
	public String save() throws Exception{
		if (null != selectId && !"".equals(selectId)) {
			transShipStatMapper.updateByPrimaryKeySelective(transShipmentStation);
		}else {			
			transShipStatMapper.insertSelective(transShipmentStation);
		}
		selectId = "";	
		clearTransShipmentStation();
		execute();
		return SUCCESS;
	}
	public String delete() throws Exception{
		if (!"".equals(selectId)) {
			String[] deleteIds = selectId.split(",");
			if (deleteIds.length == 1) {
				transShipStatMapper.deleteByPrimaryKey(selectId);
			}else{
				transShipStatMapper.deleteByPrimaryKeys(deleteIds);
			} 
		}	
		execute();
		return SUCCESS;
	}
	private void getPagination(TransShipmentStationExample example){
		int pageSize=10;
		pager.setCurrentPage(page);
		pager.setPageSize(pageSize);
		
		// 获得总记录数
		int totalSize = transShipStatMapper.countByExample(example);
		pager.setTotalCount(totalSize);
		Map<String, Integer> limitParam = new HashMap<String, Integer>();
		limitParam.put("offset", pager.getStart());
		limitParam.put("rows", pager.getPageSize());
		
		transShipList = null;		
		transShipList = transShipStatMapper.selectByExample(limitParam,example);
		pager.setItems(transShipList);
		pager.setPaginationModel(getPagerStr(totalSize));		
	}
	/**
	 * 组织分页导航栏
	 * @return
	 */
	public String  getPagerStr(int totalSize) {
		
		StringBuilder sb=new StringBuilder();
		
		int pageCount=pager.getTotalPage();//总页数
		int a_total=10;      //分页条中有多少个超链接
		
		sb.append("<table><tr><td align='center'class='wordblue'>共" + pager.getTotalCount()+"条数据&nbsp;&nbsp;页次");
		sb.append(pager.getCurrentPage()+"/" + (totalSize == 0 ? 1 : pageCount) + "页&nbsp;&nbsp;</td>");
		//实际应用中修改上面第三个参数即可

		int a_padding=(int)Math.ceil(a_total/2);  //中间的那个超链接距离边缘链接的间隔a的个数 例如：共11个分页 那么这个就是5

		if(pageCount - pager.getCurrentPage() <= a_padding && pager.getCurrentPage() > a_padding + 1) {
			a_padding=a_total-(pageCount-pager.getCurrentPage());
		}

		int start=pager.getCurrentPage()-a_padding,
		end=start+a_total;
		

		if(pager.getCurrentPage() > 1){
			sb.append("<td class='wordblue'><a href=queryTransShipStation?page=1>首页</a>&nbsp;&nbsp;</td>");
			sb.append("<td ><div class='prepage wordblue'><a href=queryTransShipStation?page="+(pager.getCurrentPage()-1)+">上一页</a></div></td>");
		}
		sb.append("<td ><div class='pagelist'>");
		if (totalSize > 0) {
			for (int i = start; i <=end; i++) {
				if(i<=0){
					end+=Math.abs(i);
					i=1;	
				}
				sb.append(" <a href=queryTransShipStation?page=" + i + " " + ( i == pager.getCurrentPage() ? "style=\"color:red;\"":"") + "> " + i + " </a>");	
				if(i==pageCount){
					break;
				}
			}
		}		
		sb.append("</div></td>");
		if(pager.getCurrentPage()-pageCount<0){
			sb.append("<td><div class='nextpage wordblue'>");
			sb.append("<a href=queryTransShipStation?page=" + ( pager.getCurrentPage() + 1 ) + ">下一页</a></div></td>");
			sb.append("<td><div class='wordblue'><a href=queryTransShipStation?page=" + pageCount + ">尾页</a></div></td>");
			}
		sb.append("</tr></table>");
		return sb.toString();
	}
	public List<TransShipmentStation> getTransShipList() {
		return transShipList;
	}
	public void setTransShipList(List<TransShipmentStation> transShipList) {
		this.transShipList = transShipList;
	}
	public String getSelectId() {
		return selectId;
	}
	public void setSelectId(String selectId) {
		this.selectId = selectId;
	}
	public void setTransShipStatMapper(
			TransShipmentStationMapper transShipStatMapper) {
		this.transShipStatMapper = transShipStatMapper;
	}
	public void setTransShipmentStation(TransShipmentStation transShipmentStation) {
		this.transShipmentStation = transShipmentStation;
	}

	public TransShipmentStation getTransShipmentStation() {
		return transShipmentStation;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
	public Pager getPager() {
		return pager;
	}

}
